package com.cn.service.server.TechnologicalStandard.Impl;

import com.cn.bean.pojo.TechnologicalStandard.*;
import com.cn.bean.pojo.bc.MaterList;
import com.cn.bean.pojo.bc.UseDefinition;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db1.bc.Mate;
import com.cn.mapper.dao.db1.bc.MaterialQualityDao;
import com.cn.mapper.dao.db1.bc.PurposesDao;
import com.cn.mapper.dao.db5.PartsManagement.BusinessDepartmentTypeDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.*;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.TechnologicalStandard.GrindingProcessStandardMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GrindingProcessStandardMainServiceImpl implements GrindingProcessStandardMainService {
    private final GrindingProcessStandardMainDao grindingProcessStandardMainDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;
    private final GrindingConsumptionAuxiliaryMaterialsDao grindingConsumptionAuxiliaryMaterialsDao;
    private final GrindingConsumptionLaborDao grindingConsumptionLaborDao;
    private final GrindingInhouseConsumptionExpenseItemDao grindingInhouseConsumptionExpenseItemDao;
    private final GrindingOutsourcedConsumptionExpenseItemDao grindingOutsourcedConsumptionExpenseItemDao;
    private final GrindingPassDao grindingPassDao;
    private final GrindingProcessStepSettingDao grindingProcessStepSettingDao;
    private final ReplacementCategoryDao replacementCategoryDao;
    private final SpecializedGrindingEquipmentSetupDao specializedGrindingEquipmentSetupDao;
    private final SpecializedGrindingTeamMainDao specializedGrindingTeamMainDao;
    private final SpecializedGrindingTeamSubDao specializedGrindingTeamSubDao;
    private final TechnicalStandardDao technicalStandardDao;
    private final PurposesDao purposesDao;
    //    private final Mate mate;
    private final MaterialQualityDao mate;
    private final TechnicalStandardHorizontalDao technicalStandardHorizontalDao;

    @Autowired(required = false)
    public GrindingProcessStandardMainServiceImpl(TechnicalStandardHorizontalDao technicalStandardHorizontalDao, MaterialQualityDao mate, PurposesDao purposesDao, TechnicalStandardDao technicalStandardDao, SpecializedGrindingTeamSubDao specializedGrindingTeamSubDao, SpecializedGrindingTeamMainDao specializedGrindingTeamMainDao, SpecializedGrindingEquipmentSetupDao specializedGrindingEquipmentSetupDao, ReplacementCategoryDao replacementCategoryDao, GrindingProcessStepSettingDao grindingProcessStepSettingDao, GrindingPassDao grindingPassDao, GrindingOutsourcedConsumptionExpenseItemDao grindingOutsourcedConsumptionExpenseItemDao, GrindingInhouseConsumptionExpenseItemDao grindingInhouseConsumptionExpenseItemDao, GrindingConsumptionLaborDao grindingConsumptionLaborDao, GrindingConsumptionAuxiliaryMaterialsDao grindingConsumptionAuxiliaryMaterialsDao, GrindingProcessStandardMainDao grindingProcessStandardMainDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.grindingProcessStandardMainDao = grindingProcessStandardMainDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
        this.grindingConsumptionAuxiliaryMaterialsDao = grindingConsumptionAuxiliaryMaterialsDao;
        this.grindingConsumptionLaborDao = grindingConsumptionLaborDao;
        this.grindingInhouseConsumptionExpenseItemDao = grindingInhouseConsumptionExpenseItemDao;
        this.grindingOutsourcedConsumptionExpenseItemDao = grindingOutsourcedConsumptionExpenseItemDao;
        this.grindingPassDao = grindingPassDao;
        this.grindingProcessStepSettingDao = grindingProcessStepSettingDao;
        this.replacementCategoryDao = replacementCategoryDao;
        this.specializedGrindingEquipmentSetupDao = specializedGrindingEquipmentSetupDao;
        this.specializedGrindingTeamMainDao = specializedGrindingTeamMainDao;
        this.specializedGrindingTeamSubDao = specializedGrindingTeamSubDao;
        this.technicalStandardDao = technicalStandardDao;
        this.purposesDao = purposesDao;
        this.mate = mate;
        this.technicalStandardHorizontalDao = technicalStandardHorizontalDao;
    }

    @Override
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMain(int page, int limit, String equipmentStructure, String structureName, String name, String standardType, String procedures) {
//        int productPurpose = firstGrindingProcessStandardMain.getProductPurpose();
//        if (productPurpose != 0) {
//            List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(firstGrindingProcessStandardMain.getProductPurpose());
//            UseDefinition useDefinition = useDefinitions.get(0);
//            firstGrindingProcessStandardMain.setProductPurposeName(useDefinition.getYtmc());
//
//        }
//        int material = firstGrindingProcessStandardMain.getMaterial();
//        if (material != 0) {
//            List<MaterList> materLists = mate.selectUseDefinitionId(firstGrindingProcessStandardMain.getMaterial());
//            MaterList mater = materLists.get(0);
//            firstGrindingProcessStandardMain.setMaterialName(mater.getYtmc());
//        }
        List<GrindingProcessStandardMain> grindingProcessStandardMains = grindingProcessStandardMainDao.selectGrindingProcessStandardMain(page, limit, equipmentStructure, structureName, name, standardType, procedures);
        for (GrindingProcessStandardMain gr : grindingProcessStandardMains) {
            int productPurpose = gr.getProductPurpose();
            if (productPurpose != 0) {
                List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(gr.getProductPurpose());
                UseDefinition useDefinition = useDefinitions.get(0);
                gr.setProductPurposeName(useDefinition.getYtmc());

            }
            int material = gr.getMaterial();
            if (material != 0) {
                List<MaterList> materLists = mate.selectUseDefinitionId(gr.getMaterial());
                MaterList mater = materLists.get(0);
                gr.setMaterialName(mater.getYtmc());
            }
        }
        return grindingProcessStandardMains;
    }

    @Override
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMainAll(int page, int limit, String equipmentStructure, String structureName, String name, String standardType, String procedures) {
        List<GrindingProcessStandardMain> grindingProcessStandardMains = grindingProcessStandardMainDao.selectGrindingProcessStandardMainAll(page, limit, equipmentStructure, structureName, name, standardType, procedures);
        for (GrindingProcessStandardMain gr : grindingProcessStandardMains) {
            int productPurpose = gr.getProductPurpose();
            if (productPurpose != 0) {
                List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(gr.getProductPurpose());
                UseDefinition useDefinition = useDefinitions.get(0);
                gr.setProductPurposeName(useDefinition.getYtmc());

            }
            int material = gr.getMaterial();
            if (material != 0) {
                List<MaterList> materLists = mate.selectUseDefinitionId(gr.getMaterial());
                MaterList mater = materLists.get(0);
                gr.setMaterialName(mater.getYtmc());
            }
        }
        return grindingProcessStandardMains;
    }

    @Override
    public Result addGrindingProcessStandardMain(GrindingProcessStandardMain grindingProcessStandardMain,
                                                 List<GrindingConsumptionAuxiliaryMaterials> grindingConsumptionAuxiliaryMaterialsList,
                                                 List<GrindingConsumptionLabor> grindingConsumptionLaborList,
                                                 List<GrindingInhouseConsumptionExpenseItem> grindingInhouseConsumptionExpenseItemList,
                                                 List<GrindingOutsourcedConsumptionExpenseItem> grindingOutsourcedConsumptionExpenseItemList,
                                                 List<GrindingPass> grindingPassList,

                                                 List<GrindingProcessStepSetting> grindingProcessStepSettingList,
                                                 List<ReplacementCategory> replacementCategoryList,
                                                 List<SpecializedGrindingEquipmentSetup> specializedGrindingEquipmentSetupList,
                                                 SpecializedGrindingTeamMain specializedGrindingTeamMain,
                                                 List<SpecializedGrindingTeamSub> specializedGrindingTeamSubList,
                                                 List<TechnicalStandard> technicalStandardList) {

        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingProcessStandardMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "GrindingProcessStandardMainTable");
        grindingProcessStandardMain.setId(ID);
        grindingProcessStandardMain.setProcessCode(code);
        grindingProcessStandardMainDao.addGrindingProcessStandardMain(grindingProcessStandardMain);
        //技术标准表
        for (TechnicalStandard technicalStandard : technicalStandardList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("TechnicalStandardTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "TechnicalStandardTable");
            technicalStandard.setId(ID1);
            technicalStandard.setM0Id(ID);
            technicalStandardDao.addTechnicalStandard(technicalStandard);
        }
        TechnicalStandardHorizontal technicalStandardHorizontal = new TechnicalStandardHorizontal();
        //存放技术标准横表：
        for (int i = 0; i < technicalStandardList.size(); i++) {
            String standard = technicalStandardList.get(i).getStandard();
            String toleranceLowerLimit = technicalStandardList.get(i).getToleranceLowerLimit();
            String toleranceUpperLimit = technicalStandardList.get(i).getToleranceUpperLimit();
            if (i == 0) {
                technicalStandardHorizontal.setGrindStandard(standard);
                technicalStandardHorizontal.setGrindToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setGrindToleranceUpper(toleranceUpperLimit);
            }
            if (i == 1) {
                technicalStandardHorizontal.setRoughnessStandard(standard);
                technicalStandardHorizontal.setRoughnessToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setRoughnessToleranceUpper(toleranceUpperLimit);

            }
            if (i == 2) {
                technicalStandardHorizontal.setLateralShiftStandard(standard);
                technicalStandardHorizontal.setLateralShiftToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setLateralShiftToleranceUpper(toleranceUpperLimit);

            }
            if (i == 3) {
                technicalStandardHorizontal.setConeHeightStandard(standard);
                technicalStandardHorizontal.setConeLengthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeLengthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 4) {
                technicalStandardHorizontal.setConeLengthStandard(standard);
                technicalStandardHorizontal.setConeLengthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeLengthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 5) {
                technicalStandardHorizontal.setConeHeightRatioStandard(standard);
                technicalStandardHorizontal.setConeHeightRatioToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeHeightRatioToleranceUpper(toleranceUpperLimit);
            }
            if (i == 6) {
                technicalStandardHorizontal.setSteelStripWidthStandard(standard);
                technicalStandardHorizontal.setSteelStripWidthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setSteelStripWidthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 7) {
                technicalStandardHorizontal.setConsumption(standard);
                technicalStandardHorizontal.setConsumption_lower(toleranceLowerLimit);
                technicalStandardHorizontal.setConsumption_upper(toleranceUpperLimit);
            }

        }
        technicalStandardHorizontal.setId(ID);
        technicalStandardHorizontal.setM0Id(ID);
        technicalStandardHorizontalDao.addTechnicalStandardHorizontal(technicalStandardHorizontal);
        //专用磨削班组主表
        Sys_Sequence_Info_T businessTypeTable2 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingTeamMainTable");
        int presentValue2 = businessTypeTable2.getPresentValue();
        int stepLength2 = businessTypeTable2.getStepLength();
        int ID2 = presentValue2 + stepLength2;
        String code2 = String.valueOf(ID);
        if (ID2 > businessTypeTable2.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID2, "SpecializedGrindingTeamMainTable");
        specializedGrindingTeamMain.setId(ID2);
        specializedGrindingTeamMain.setM0Id(ID);
        specializedGrindingTeamMainDao.addSpecializedGrindingTeamMain(specializedGrindingTeamMain);
        //专用磨削班组子表
        for (SpecializedGrindingTeamSub specializedGrindingTeamSub : specializedGrindingTeamSubList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingTeamSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "SpecializedGrindingTeamSubTable");
            specializedGrindingTeamSub.setId(ID1);
            specializedGrindingTeamSub.setM1Id(ID2);
            specializedGrindingTeamSubDao.addSpecializedGrindingTeamSub(specializedGrindingTeamSub);
        }

        //专用磨削设备设定
        for (SpecializedGrindingEquipmentSetup specializedGrindingEquipmentSetup : specializedGrindingEquipmentSetupList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingEquipmentSetupTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "SpecializedGrindingEquipmentSetupTable");
            specializedGrindingEquipmentSetup.setId(ID1);
            specializedGrindingEquipmentSetup.setM0Id(ID);
            specializedGrindingEquipmentSetupDao.addSpecializedGrindingEquipmentSetup(specializedGrindingEquipmentSetup);
        }

        //更换类别
        for (ReplacementCategory replacementCategory : replacementCategoryList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ReplacementCategoryTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "ReplacementCategoryTable");
            replacementCategory.setId(ID1);
            replacementCategory.setM0Id(ID);
            replacementCategoryDao.addReplacementCategory(replacementCategory);
        }
        //磨削工步设置表
        for (GrindingProcessStepSetting grindingProcessStepSetting : grindingProcessStepSettingList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingProcessStepSettingTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingProcessStepSettingTable");
            grindingProcessStepSetting.setId(ID1);
            grindingProcessStepSetting.setM0Id(ID);
            grindingProcessStepSettingDao.addGrindingProcessStepSetting(grindingProcessStepSetting);
        }

        //磨削道次
        for (GrindingPass grindingPass : grindingPassList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingPassTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingPassTable");
            grindingPass.setId(ID1);
            grindingPass.setM0Id(ID);
            grindingPassDao.addGrindingPass(grindingPass);
        }
        //磨削（委外）耗用费用项目
        for (GrindingOutsourcedConsumptionExpenseItem grindingOutsourcedConsumptionExpenseItem : grindingOutsourcedConsumptionExpenseItemList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingOutsourcedConsumptionExpenseItemTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingOutsourcedConsumptionExpenseItemTable");
            grindingOutsourcedConsumptionExpenseItem.setId(ID1);
            grindingOutsourcedConsumptionExpenseItem.setM0Id(ID);
            grindingOutsourcedConsumptionExpenseItemDao.addGrindingOutsourcedConsumptionExpenseItem(grindingOutsourcedConsumptionExpenseItem);
        }


        //磨削（自制）耗用费用项目
        for (GrindingInhouseConsumptionExpenseItem grindingInhouseConsumptionExpenseItem : grindingInhouseConsumptionExpenseItemList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingInhouseConsumptionExpenseItemTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingInhouseConsumptionExpenseItemTable");
            grindingInhouseConsumptionExpenseItem.setId(ID1);
            grindingInhouseConsumptionExpenseItem.setM0Id(ID);
            grindingInhouseConsumptionExpenseItemDao.addGrindingInhouseConsumptionExpenseItem(grindingInhouseConsumptionExpenseItem);
        }


        //磨削耗用人工
        for (GrindingConsumptionLabor grindingConsumptionLabor : grindingConsumptionLaborList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingConsumptionLaborTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingConsumptionLaborTable");
            grindingConsumptionLabor.setId(ID1);
            grindingConsumptionLabor.setM0Id(ID);
            grindingConsumptionLaborDao.addGrindingConsumptionLabor(grindingConsumptionLabor);
        }


        //磨削耗用辅助材料表
        for (GrindingConsumptionAuxiliaryMaterials grindingConsumptionAuxiliaryMaterials : grindingConsumptionAuxiliaryMaterialsList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingConsumptionAuxiliaryMaterialsTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingConsumptionAuxiliaryMaterialsTable");
            grindingConsumptionAuxiliaryMaterials.setId(ID1);
            grindingConsumptionAuxiliaryMaterials.setM0Id(ID);
            grindingConsumptionAuxiliaryMaterialsDao.addGrindingConsumptionAuxiliaryMaterials(grindingConsumptionAuxiliaryMaterials);
        }

        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteGrindingProcessStandardMain(List<GrindingProcessStandardMain> idList) {
        for (GrindingProcessStandardMain grindingProcessStandardMain : idList) {
            int id = grindingProcessStandardMain.getId();
            Integer integer = grindingProcessStandardMainDao.selectShZT(id);
            if (integer > 0) {
                return Result.failure("500", "已审核的数据不能进行删除操作");
            }
        }
        for (GrindingProcessStandardMain grindingProcessStandardMain : idList) {
            int id = grindingProcessStandardMain.getId();
            grindingConsumptionAuxiliaryMaterialsDao.deleteGrindingConsumptionAuxiliaryMaterials(id);
            grindingConsumptionLaborDao.deleteGrindingConsumptionLabor(id);
            grindingInhouseConsumptionExpenseItemDao.deleteGrindingInhouseConsumptionExpenseItem(id);
            grindingOutsourcedConsumptionExpenseItemDao.deleteGrindingOutsourcedConsumptionExpenseItem(id);
            grindingPassDao.deleteGrindingPass(id);
            grindingProcessStepSettingDao.deleteGrindingProcessStepSetting(id);
            replacementCategoryDao.deleteReplacementCategory(id);
            specializedGrindingEquipmentSetupDao.deleteSpecializedGrindingEquipmentSetup(id);
            specializedGrindingTeamMainDao.deleteSpecializedGrindingTeamMain(id);
            List<SpecializedGrindingTeamMain> specializedGrindingTeamMains = specializedGrindingTeamMainDao.selectSpecializedGrindingTeamMainMid(id);
            if (specializedGrindingTeamMains.size() != 0) {
                SpecializedGrindingTeamMain specializedGrindingTeamMain = specializedGrindingTeamMains.get(0);
                int id1 = specializedGrindingTeamMain.getId();
                specializedGrindingTeamSubDao.deleteSpecializedGrindingTeamSub(id1);
            }
            technicalStandardDao.deleteTechnicalStandard(id);
            technicalStandardHorizontalDao.deleteTechnicalStandardHorizontal(id);
            grindingProcessStandardMainDao.deleteGrindingProcessStandardMain(id);

        }

        return Result.success("200", "删除成功");
    }

    @Override
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMainId(int id) {
//       主表
        // 主表
        List<GrindingProcessStandardMain> grindingProcessStandardMains = grindingProcessStandardMainDao.selectGrindingProcessStandardMainId(id);


        if (!grindingProcessStandardMains.isEmpty()) {
            GrindingProcessStandardMain firstGrindingProcessStandardMain = grindingProcessStandardMains.get(0);
            int productPurpose = firstGrindingProcessStandardMain.getProductPurpose();
            if (productPurpose != 0) {
                List<UseDefinition> useDefinitions = purposesDao.selectUseDefinitionId(firstGrindingProcessStandardMain.getProductPurpose());
                UseDefinition useDefinition = useDefinitions.get(0);
                firstGrindingProcessStandardMain.setProductPurposeName(useDefinition.getYtmc());

            }
            int material = firstGrindingProcessStandardMain.getMaterial();
            if (material != 0) {
                List<MaterList> materLists = mate.selectUseDefinitionId(firstGrindingProcessStandardMain.getMaterial());
                MaterList mater = materLists.get(0);
                firstGrindingProcessStandardMain.setMaterialName(mater.getYtmc());
            }

            List<GrindingConsumptionAuxiliaryMaterials> grindingConsumptionAuxiliaryMaterialsList = grindingConsumptionAuxiliaryMaterialsDao.selectGrindingConsumptionAuxiliaryMaterialsId(id);
            List<GrindingConsumptionLabor> grindingConsumptionLaborList = grindingConsumptionLaborDao.selectGrindingConsumptionLaborId(id);
            List<GrindingInhouseConsumptionExpenseItem> grindingInhouseConsumptionExpenseItemList = grindingInhouseConsumptionExpenseItemDao.selectGrindingInhouseConsumptionExpenseItemId(id);
            List<GrindingOutsourcedConsumptionExpenseItem> grindingOutsourcedConsumptionExpenseItemList = grindingOutsourcedConsumptionExpenseItemDao.selectGrindingOutsourcedConsumptionExpenseItem(id);
            List<GrindingPass> grindingPassList = grindingPassDao.selectGrindingPassId(id);
            List<GrindingProcessStepSetting> grindingProcessStepSettingList = grindingProcessStepSettingDao.selectGrindingProcessStepSettingId(id);
            List<ReplacementCategory> replacementCategoryList = replacementCategoryDao.selectReplacementCategoryId(id);
            List<SpecializedGrindingEquipmentSetup> specializedGrindingEquipmentSetups = specializedGrindingEquipmentSetupDao.selectSpecializedGrindingEquipmentSetupId(id);
            SpecializedGrindingTeamMain specializedGrindingTeamMain = specializedGrindingTeamMainDao.selectSpecializedGrindingTeamMainMid(id).get(0);
            List<SpecializedGrindingTeamSub> specializedGrindingTeamSubList = specializedGrindingTeamSubDao.selectSpecializedGrindingTeamSubId(specializedGrindingTeamMain.getId());
            List<TechnicalStandard> technicalStandards = technicalStandardDao.selectTechnicalStandardId(id);


            firstGrindingProcessStandardMain.setGrindingConsumptionAuxiliaryMaterialsList(grindingConsumptionAuxiliaryMaterialsList);
            firstGrindingProcessStandardMain.setGrindingConsumptionLaborList(grindingConsumptionLaborList);
            firstGrindingProcessStandardMain.setGrindingInhouseConsumptionExpenseItemList(grindingInhouseConsumptionExpenseItemList);
            firstGrindingProcessStandardMain.setGrindingOutsourcedConsumptionExpenseItemList(grindingOutsourcedConsumptionExpenseItemList);
            firstGrindingProcessStandardMain.setGrindingPassList(grindingPassList);
            firstGrindingProcessStandardMain.setGrindingProcessStepSettingList(grindingProcessStepSettingList);
            firstGrindingProcessStandardMain.setReplacementCategoryList(replacementCategoryList);
            firstGrindingProcessStandardMain.setSpecializedGrindingEquipmentSetupList(specializedGrindingEquipmentSetups);
            firstGrindingProcessStandardMain.setSpecializedGrindingTeamMain(specializedGrindingTeamMain);
            firstGrindingProcessStandardMain.setSpecializedGrindingTeamSubList(specializedGrindingTeamSubList);
            firstGrindingProcessStandardMain.setTechnicalStandardList(technicalStandards);
        }

        return grindingProcessStandardMains;
    }

    @Override
    public Result updateGrindingProcessStandardMain(GrindingProcessStandardMain grindingProcessStandardMain, List<GrindingConsumptionAuxiliaryMaterials> grindingConsumptionAuxiliaryMaterialsList, List<GrindingConsumptionLabor> grindingConsumptionLaborList, List<GrindingInhouseConsumptionExpenseItem> grindingInhouseConsumptionExpenseItemList, List<GrindingOutsourcedConsumptionExpenseItem> grindingOutsourcedConsumptionExpenseItemList, List<GrindingPass> grindingPassList, List<GrindingProcessStepSetting> grindingProcessStepSettingList, List<ReplacementCategory> replacementCategoryList, List<SpecializedGrindingEquipmentSetup> specializedGrindingEquipmentSetupList, SpecializedGrindingTeamMain specializedGrindingTeamMain, List<SpecializedGrindingTeamSub> specializedGrindingTeamSubList, List<TechnicalStandard> technicalStandardList) {
        int id = grindingProcessStandardMain.getId();
        grindingConsumptionAuxiliaryMaterialsDao.deleteGrindingConsumptionAuxiliaryMaterials(id);
        grindingConsumptionLaborDao.deleteGrindingConsumptionLabor(id);
        grindingInhouseConsumptionExpenseItemDao.deleteGrindingInhouseConsumptionExpenseItem(id);
        grindingOutsourcedConsumptionExpenseItemDao.deleteGrindingOutsourcedConsumptionExpenseItem(id);
        grindingPassDao.deleteGrindingPass(id);
        grindingProcessStepSettingDao.deleteGrindingProcessStepSetting(id);
        replacementCategoryDao.deleteReplacementCategory(id);
        specializedGrindingEquipmentSetupDao.deleteSpecializedGrindingEquipmentSetup(id);
        specializedGrindingTeamMainDao.deleteSpecializedGrindingTeamMain(id);
        List<SpecializedGrindingTeamMain> specializedGrindingTeamMains = specializedGrindingTeamMainDao.selectSpecializedGrindingTeamMainMid(id);
        if (specializedGrindingTeamMains.size() != 0) {
            SpecializedGrindingTeamMain specializedGrindingTeamMain1 = specializedGrindingTeamMains.get(0);
            int id1 = specializedGrindingTeamMain1.getId();
            specializedGrindingTeamSubDao.deleteSpecializedGrindingTeamSub(id1);
        }
//            specializedGrindingTeamSubDao.deleteSpecializedGrindingTeamSub(specializedGrindingTeamMainDao.selectSpecializedGrindingTeamMainMid(id).get(0).getId());
//            SpecializedGrindingTeamMain specializedGrindingTeamMain = specializedGrindingTeamMainDao.selectSpecializedGrindingTeamMainMid(id).get(0);
//            specializedGrindingTeamSubDao.deleteSpecializedGrindingTeamSub(specializedGrindingTeamMain.getId());
        technicalStandardDao.deleteTechnicalStandard(id);
        technicalStandardHorizontalDao.deleteTechnicalStandardHorizontal(id);
//        grindingProcessStandardMainDao.deleteGrindingProcessStandardMain(id);
        //技术标准表
        for (TechnicalStandard technicalStandard : technicalStandardList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("TechnicalStandardTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "TechnicalStandardTable");
            technicalStandard.setId(ID1);
            technicalStandard.setM0Id(id);
            technicalStandardDao.addTechnicalStandard(technicalStandard);
        }
        TechnicalStandardHorizontal technicalStandardHorizontal = new TechnicalStandardHorizontal();
        //存放技术标准横表：
        for (int i = 0; i < technicalStandardList.size(); i++) {
            String standard = technicalStandardList.get(i).getStandard();
            String toleranceLowerLimit = technicalStandardList.get(i).getToleranceLowerLimit();
            String toleranceUpperLimit = technicalStandardList.get(i).getToleranceUpperLimit();
            if (i == 0) {
                technicalStandardHorizontal.setGrindStandard(standard);
                technicalStandardHorizontal.setGrindToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setGrindToleranceUpper(toleranceUpperLimit);
            }
            if (i == 1) {
                technicalStandardHorizontal.setRoughnessStandard(standard);
                technicalStandardHorizontal.setRoughnessToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setRoughnessToleranceUpper(toleranceUpperLimit);

            }
            if (i == 2) {
                technicalStandardHorizontal.setLateralShiftStandard(standard);
                technicalStandardHorizontal.setLateralShiftToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setLateralShiftToleranceUpper(toleranceUpperLimit);

            }
            if (i == 3) {
                technicalStandardHorizontal.setConeHeightStandard(standard);
                technicalStandardHorizontal.setConeLengthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeLengthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 4) {
                technicalStandardHorizontal.setConeLengthStandard(standard);
                technicalStandardHorizontal.setConeLengthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeLengthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 5) {
                technicalStandardHorizontal.setConeHeightRatioStandard(standard);
                technicalStandardHorizontal.setConeHeightRatioToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setConeHeightRatioToleranceUpper(toleranceUpperLimit);
            }
            if (i == 6) {
                technicalStandardHorizontal.setSteelStripWidthStandard(standard);
                technicalStandardHorizontal.setSteelStripWidthToleranceLower(toleranceLowerLimit);
                technicalStandardHorizontal.setSteelStripWidthToleranceUpper(toleranceUpperLimit);
            }
            if (i == 7) {
                technicalStandardHorizontal.setConsumption(standard);
                technicalStandardHorizontal.setConsumption_lower(toleranceLowerLimit);
                technicalStandardHorizontal.setConsumption_upper(toleranceUpperLimit);
            }

        }
        technicalStandardHorizontal.setId(id);
        technicalStandardHorizontal.setM0Id(id);
        technicalStandardHorizontalDao.addTechnicalStandardHorizontal(technicalStandardHorizontal);

        //专用磨削班组主表
        Sys_Sequence_Info_T businessTypeTable2 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingTeamMainTable");
        int presentValue2 = businessTypeTable2.getPresentValue();
        int stepLength2 = businessTypeTable2.getStepLength();
        int ID2 = presentValue2 + stepLength2;
        String code2 = String.valueOf(id);
        if (ID2 > businessTypeTable2.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID2, "SpecializedGrindingTeamMainTable");
        specializedGrindingTeamMain.setId(ID2);
        specializedGrindingTeamMain.setM0Id(id);
        specializedGrindingTeamMainDao.addSpecializedGrindingTeamMain(specializedGrindingTeamMain);
        //专用磨削班组子表
        for (SpecializedGrindingTeamSub specializedGrindingTeamSub : specializedGrindingTeamSubList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingTeamSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "SpecializedGrindingTeamSubTable");
            specializedGrindingTeamSub.setId(ID1);
            specializedGrindingTeamSub.setM1Id(ID2);
            specializedGrindingTeamSubDao.addSpecializedGrindingTeamSub(specializedGrindingTeamSub);
        }

        //专用磨削设备设定
        for (SpecializedGrindingEquipmentSetup specializedGrindingEquipmentSetup : specializedGrindingEquipmentSetupList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("SpecializedGrindingEquipmentSetupTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "SpecializedGrindingEquipmentSetupTable");
            specializedGrindingEquipmentSetup.setId(ID1);
            specializedGrindingEquipmentSetup.setM0Id(id);
            specializedGrindingEquipmentSetupDao.addSpecializedGrindingEquipmentSetup(specializedGrindingEquipmentSetup);
        }

        //更换类别
        for (ReplacementCategory replacementCategory : replacementCategoryList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("ReplacementCategoryTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "ReplacementCategoryTable");
            replacementCategory.setId(ID1);
            replacementCategory.setM0Id(id);
            replacementCategoryDao.addReplacementCategory(replacementCategory);
        }
        //磨削工步设置表
        for (GrindingProcessStepSetting grindingProcessStepSetting : grindingProcessStepSettingList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingProcessStepSettingTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingProcessStepSettingTable");
            grindingProcessStepSetting.setId(ID1);
            grindingProcessStepSetting.setM0Id(id);
            grindingProcessStepSettingDao.addGrindingProcessStepSetting(grindingProcessStepSetting);
        }

        //磨削道次
        for (GrindingPass grindingPass : grindingPassList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingPassTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingPassTable");
            grindingPass.setId(ID1);
            grindingPass.setM0Id(id);
            grindingPassDao.addGrindingPass(grindingPass);
        }
        //磨削（委外）耗用费用项目
        for (GrindingOutsourcedConsumptionExpenseItem grindingOutsourcedConsumptionExpenseItem : grindingOutsourcedConsumptionExpenseItemList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingOutsourcedConsumptionExpenseItemTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingOutsourcedConsumptionExpenseItemTable");
            grindingOutsourcedConsumptionExpenseItem.setId(ID1);
            grindingOutsourcedConsumptionExpenseItem.setM0Id(id);
            grindingOutsourcedConsumptionExpenseItemDao.addGrindingOutsourcedConsumptionExpenseItem(grindingOutsourcedConsumptionExpenseItem);
        }


        //磨削（自制）耗用费用项目
        for (GrindingInhouseConsumptionExpenseItem grindingInhouseConsumptionExpenseItem : grindingInhouseConsumptionExpenseItemList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingInhouseConsumptionExpenseItemTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingInhouseConsumptionExpenseItemTable");
            grindingInhouseConsumptionExpenseItem.setId(ID1);
            grindingInhouseConsumptionExpenseItem.setM0Id(id);
            grindingInhouseConsumptionExpenseItemDao.addGrindingInhouseConsumptionExpenseItem(grindingInhouseConsumptionExpenseItem);
        }


        //磨削耗用人工
        for (GrindingConsumptionLabor grindingConsumptionLabor : grindingConsumptionLaborList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingConsumptionLaborTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingConsumptionLaborTable");
            grindingConsumptionLabor.setId(ID1);
            grindingConsumptionLabor.setM0Id(id);
            grindingConsumptionLaborDao.addGrindingConsumptionLabor(grindingConsumptionLabor);
        }


        //磨削耗用辅助材料表
        for (GrindingConsumptionAuxiliaryMaterials grindingConsumptionAuxiliaryMaterials : grindingConsumptionAuxiliaryMaterialsList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("GrindingConsumptionAuxiliaryMaterialsTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "GrindingConsumptionAuxiliaryMaterialsTable");
            grindingConsumptionAuxiliaryMaterials.setId(ID1);
            grindingConsumptionAuxiliaryMaterials.setM0Id(id);
            grindingConsumptionAuxiliaryMaterialsDao.addGrindingConsumptionAuxiliaryMaterials(grindingConsumptionAuxiliaryMaterials);
        }
        grindingProcessStandardMainDao.updateGrindingProcessStandardMain(grindingProcessStandardMain);
        return Result.success("200", "修改成功");
    }

    @Override
    public Result updateShGrindingProcessStandardMain(List<GrindingProcessStandardMain> grindingProcessStandardMainList, int state, String reviewer, String reviewerID, String reviewTime) {
        for (GrindingProcessStandardMain grindingProcessStandardMain : grindingProcessStandardMainList) {
            grindingProcessStandardMainDao.updateShGrindingProcessStandardMain(grindingProcessStandardMain.getId(), state, reviewer, reviewerID, reviewTime);
        }

        if (state == 1) {
            return Result.success("200", "审核成功");

        }
        return Result.success("200", "弃审成功");
    }

    @Override
    public List<GrindingProcessStandardMain> selectGmModel(int page, int limit, String model) {
        return grindingProcessStandardMainDao.selectGmModel(page, limit, model);
    }


}
